import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  ClassSerializerInterceptor,
  UseInterceptors,
  UseGuards,
} from '@nestjs/common';
import { TeacherService } from './teacher.service';
import { CreateTeacherDto } from './dto/create-teacher.dto';
import { UpdateTeacherDto } from './dto/update-teacher.dto';
import {
  ApiBody,
  ApiOperation,
  ApiQuery,
  ApiResponse,
  ApiTags,
} from '@nestjs/swagger';
import { JwtGuard } from '../guards/jwt.guards';
import { CreateTeacherDtoPipe } from './pipe/created-teacher.dto.pipe';

@Controller('teacher')
@UseInterceptors(ClassSerializerInterceptor)
@UseGuards(JwtGuard) // 校验token是否通过
@ApiTags('教师模块')
export class TeacherController {
  constructor(private readonly teacherService: TeacherService) {}

  @Post()
  @ApiOperation({ summary: '创建教师' })
  @ApiBody({ type: CreateTeacherDto, description: '' })
  @ApiResponse({
    status: 200,
    description: '成功返回200，失败返回400',
    type: CreateTeacherDto,
  })
  create(@Body(CreateTeacherDtoPipe) createTeacherDto: CreateTeacherDto) {
    return this.teacherService.create(createTeacherDto);
  }

  @Get()
  findAll() {
    return this.teacherService.findAll();
  }

  @Get(':id')
  @ApiOperation({ summary: '教师详情' })
  @ApiQuery({ name: 'id', required: true, description: '教师id' })
  @ApiResponse({
    status: 200,
    description: '成功返回200，失败返回400',
    type: CreateTeacherDto,
  })
  findOne(@Param('id') id: number) {
    return this.teacherService.findDetail(id);
  }

  @ApiOperation({ summary: '教师更新' })
  @ApiQuery({ name: 'id', required: true, description: '教师id' })
  @ApiBody({ type: CreateTeacherDto, description: '' })
  @ApiResponse({
    status: 200,
    description: '成功返回200，失败返回400',
    type: CreateTeacherDto,
  })
  @Patch(':id')
  update(@Param('id') id: number, @Body() updateTeacherDto: UpdateTeacherDto) {
    return this.teacherService.update(id, updateTeacherDto);
  }

  @Delete(':id')
  @ApiOperation({ summary: '教师删除' })
  @ApiQuery({
    name: 'id',
    required: true,
    description: '教师id，删除老师的同时会删除与教师相关联的数据',
  })
  @ApiResponse({
    status: 200,
    description: '成功返回200，失败返回400',
    type: CreateTeacherDto,
  })
  remove(@Param('id') id: number) {
    return this.teacherService.remove(id);
  }
}
